package com.yfeng.common.utils;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import sun.misc.BASE64Decoder;

/**
 * 编码,解码通用接口
 *
 * 
 */
public class DecodeUtil {

    private final static Log log = LogFactory.getLog(DecodeUtil.class);

    /**
     * 将字符串s 进行 BASE64 编码
     */
    public static String getBASE64(String s) {
        if (s == null)
            return null;
        return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
    }

    /**
     * 将 BASE64 编码的字符串 s 进行解码
     */
    public static String getFromBASE64(String s) {
        if (s == null)
            return null;

        byte[] b = getByteFromBASE64(s);
        return b != null ? new String(b) : null;

    }

    /**
     * 将BASE64编码的字符串解码成byte[]
     *
     * @param s
     * @return byte[]
     *        
     */
    public static byte[] getByteFromBASE64(String s) {
        if (s == null)
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] b = null;
        try {
            b = decoder.decodeBuffer(s);
        } catch (IOException e) {
            log.error(e.getMessage(),e);
            log.info("-----------getByteFromBASE64------------" + e.getMessage());
        }
        return b;
	}

}
